package com.ayoka.parenting.database;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

import com.ayoka.parenting.bean.PCASBean;
import com.jincp.common.base.BaseApplication;

import java.util.List;

/**
 * Created by Administrator on 2015/7/9.
 */
public class DBManager {
    private static DBHelper helper;
    private static DBManager instance;

    public static DBManager getInstance() {
        if (instance == null) {
            synchronized (DBManager.class) {
                instance = new DBManager();
            }
        }
        return instance;
    }

    private DBManager() {
        helper = new DBHelper(BaseApplication.getInstance());
    }

    /**
     * 插入省市区地址数据
     */
    public synchronized void insertPCAS(List<PCASBean> PCASBeanList) {
        SQLiteDatabase database = helper.getWritableDatabase();
        database.beginTransaction();
        try {
            for (PCASBean addressBean : PCASBeanList) {
                ContentValues values = new ContentValues();
                values.put(PCASEntity.COLUMN_LABEL, addressBean.label);
                values.put(PCASEntity.COLUMN_PARENT, addressBean.parent);
                values.put(PCASEntity.COLUMN_VALUE, addressBean.value);
                database.replace(PCASEntity.TABLE_NAME, null, values);
            }
            database.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            database.endTransaction();
            database.close();
        }
    }
}
